package com.hxkcloud.nurse.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.activitys.AssessRecordListActivity;
import com.hxkcloud.nurse.beans.PushRecordBean;
import com.hxkcloud.nurse.utils.ImageOptionUtils;
import com.hxkcloud.nurse.views.CircleImageView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/1/11.
 */

public class PushRecordAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<PushRecordBean> pushRecordBeanList;
    private Context mContext;

    public PushRecordAdapter(List<PushRecordBean> pushRecordBeanList, Context mContext) {
        this.pushRecordBeanList = pushRecordBeanList;
        this.mContext = mContext;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.adpter_push_record_item, null);
        PushRecordHolder recordHolder = new PushRecordHolder(view);

        return recordHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        PushRecordHolder viewHolder = (PushRecordHolder) holder;
        ImageLoader.getInstance().displayImage(pushRecordBeanList.get(position).getHeadImage(),
                viewHolder.img_icon, ImageOptionUtils.getInstance().getMan20HeadOptions());
        viewHolder.tv_age.setText(pushRecordBeanList.get(position).getAge() + "岁");
        viewHolder.tv_name.setText(pushRecordBeanList.get(position).getName());
        if (pushRecordBeanList.get(position).getSex().equals("1")) {
            viewHolder.tv_sex.setText("女");
        } else {
            viewHolder.tv_sex.setText("男");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(new Date(pushRecordBeanList.get(position).getSendTime()));
        viewHolder.tv_time.setText(date);
        String type = "";
        if (pushRecordBeanList.get(position).getAnswerType() != null && !pushRecordBeanList.get(position).getAnswerType().equals("2")) {//患者
            if (pushRecordBeanList.get(position).getQuestionStatus().equals("0")) {
                type = "<font color='#40d2e0'>患者自评(患者未答题)</font>";
            } else {
                type = "<font color='#cccccc'>患者自评</font>";
            }
        } else {
            type = "<font color='#cccccc'>护士测评</font>";
        }
        viewHolder.tv_type.setText(Html.fromHtml(type));
        viewHolder.linner_click.setTag(position);
        viewHolder.linner_click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int index = (int) v.getTag();
                Intent intent = new Intent(mContext, AssessRecordListActivity.class);
                intent.putExtra("patientId", pushRecordBeanList.get(index).getPatientId());
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return pushRecordBeanList.size();
    }

    class PushRecordHolder extends RecyclerView.ViewHolder {
        public CircleImageView img_icon;
        public TextView tv_name;
        public TextView tv_time;
        public TextView tv_sex;
        public TextView tv_age;
        public TextView tv_type;
        public LinearLayout linner_click;

        public PushRecordHolder(View itemView) {
            super(itemView);
            linner_click = (LinearLayout) itemView.findViewById(R.id.linner_click);
            img_icon = (CircleImageView) itemView.findViewById(R.id.img_icon);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            tv_time = (TextView) itemView.findViewById(R.id.tv_time);
            tv_sex = (TextView) itemView.findViewById(R.id.tv_sex);
            tv_age = (TextView) itemView.findViewById(R.id.tv_age);
            tv_type = (TextView) itemView.findViewById(R.id.tv_type);

        }
    }
}
